package com.thermofisher.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.thermofisher.beans.Config;
import com.thermofisher.beans.EmailRecord;
import com.thermofisher.service.ConfigService;
import com.thermofisher.service.EmailRecordService;
import com.thermofisher.utils.MailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class B1000Mail
{

    private final Logger logger = LoggerFactory.getLogger(B1000Mail.class);

    @Resource
    private EmailRecordService emailRecordService;

    @Resource
    private ConfigService configService;

    @Resource
    private MailUtil mailUtil;

    public void send()
    {
        //获取未发送的邮件

        List<EmailRecord> emailList = emailRecordService.getAnsweredEmail();
        if (!ObjectUtils.isEmpty(emailList))
        {
            Config head = configService.getOne(
                    Wrappers.<Config>lambdaQuery()
                            .eq(Config::getPublicKey, "template1_head"));
            Config body = configService.getOne(
                    Wrappers.<Config>lambdaQuery()
                            .eq(Config::getPublicKey, "template1_body"));
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = format.format(date);
            for (EmailRecord emailRecord : emailList)
            {
                int times = emailRecord.getTimes();
                String head1 = head.getValue();
                String head2 = head.getRemark();
                String body1 = body.getRemark();
                head1 = head1.replace("####", emailRecord.getAliaName());
                head2 = head2.replaceFirst("####", emailRecord.getStatusChange());
                head2 = head2.replaceFirst("####", emailRecord.getProjectNo());
                head2 = head2.replaceFirst("####", emailRecord.getProjectName());

              //  head2 = head2.replaceFirst("####", emailRecord.getUpdateDate().substring(0,emailRecord.getUpdateDate().length()-9)).replace("T"," ");
                try
                {
                    System.out.println(head1 + head2 + body1);
                    mailUtil.sendHtmlMail(emailRecord.getEmail(), "Thermo Fisher Project Change",
                                          head1 + head2 + body1);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    emailRecord.setTimes(times + 1);
                    emailRecord.setSendStatus(1);
                    emailRecord.setSendDate(dateString);
                    break;
                }
                emailRecord.setTimes(times + 1);
                emailRecord.setSendStatus(2);
                emailRecord.setSendDate(dateString);

            }
            emailRecordService.updateBatchById(emailList);
        }
    }

}
